home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1843 / 1843.xpi / content / firebug / traceConsole.js < prev    next >
Text File  |  2010-01-15  |  10KB  |  306 lines

  1. /* See license.txt for terms of usage */
  2.  
  3. // ************************************************************************************************
  4. // Shorcuts and Services
  5.  
  6. const Cc = Components.classes;
  7. const Ci = Components.interfaces;
  8.  
  9. const traceService = Cc["@joehewitt.com/firebug-trace-service;1"].getService(Ci.nsIObserverService);
  10.  
  11. const PrefService = Cc["@mozilla.org/preferences-service;1"];
  12. const prefs = PrefService.getService(Ci.nsIPrefBranch2);
  13. const prefService = PrefService.getService(Ci.nsIPrefService);
  14.  
  15. var gFindBar;
  16.  
  17. const reDBG = /extensions\.([^\.]*)\.(DBG_.*)/;
  18. const reDBG_FBS = /DBG_FBS_(.*)/;
  19. const reEndings = /\r\n|\r|\n/;
  20.  
  21. // The lib.js isn't included in this window so, define the global here.
  22. // It'll be initialized from window parameters (see initialize method).
  23. var FBL;
  24.  
  25. // Cache messages that are fired before the content of the window is loaded.
  26. var queue = [];
  27.  
  28. // ************************************************************************************************
  29. // Trace Window Implementation
  30.  
  31. var TraceConsole =
  32. {
  33.     modules: [],
  34.  
  35.     initialize: function()
  36.     {
  37.         var args = window.arguments[0];
  38.         FBL = args.FBL;
  39.         Firebug = args.Firebug;
  40.  
  41.         // Get pref domain is used for message filtering. Only logs that belong
  42.         // to this pref-domain will be displayed.
  43.         this.prefDomain = args.prefDomain;
  44.         document.title = FBL.$STR("title.Tracing") + ": " + this.prefDomain;
  45.  
  46.         // Register listeners and observers
  47.         traceService.addObserver(this, "firebug-trace-on-message", false);
  48.         prefs.addObserver(this.prefDomain, this, false);
  49.  
  50.         // Initialize root node of the trace-console window.
  51.         var consoleFrame = document.getElementById("consoleFrame");
  52.         this.consoleNode = consoleFrame.contentDocument.getElementById("panelNode-traceConsole");
  53.         Firebug.TraceModule.CommonBaseUI.initializeContent(this.consoleNode, this, this.prefDomain,
  54.             FBL.bind(this.initializeContent, this));
  55.  
  56.         gFindBar = document.getElementById("FindToolbar");
  57.     },
  58.  
  59.     initializeContent: function(logNode)
  60.     {
  61.         this.logs = logNode;
  62.  
  63.         // Notify listeners
  64.         Firebug.TraceModule.onLoadConsole(window, logNode);
  65.         this.registerModule(Firebug.TraceModule);
  66.  
  67.         // Make sure the UI is localized.
  68.         this.internationalizeUI();
  69.         this.updateTimeInfo();
  70.  
  71.         // If the opener is closed the console must be also closed.
  72.         // (this console uses shared object from the opener (e.g. Firebug)
  73.         window.opener.addEventListener("close", this.onCloseOpener, true);
  74.         this.addedOnCloseOpener = true;
  75.  
  76.         // Fetch all cached messages.
  77.         for (var i=0; i<queue.length; i++)
  78.             this.dump(queue[i]);
  79.     },
  80.  
  81.     internationalizeUI: function()
  82.     {
  83.         var buttons = ["clearConsole", "findConsole", "separateConsole",
  84.             "restartFirefox", "closeFirefox", "saveToFile"];
  85.  
  86.         for (var i=0; i<buttons.length; i++)
  87.         {
  88.             var element = document.getElementById(buttons[i]);
  89.             FBL.internationalize(element, "label");
  90.             FBL.internationalize(element, "tooltiptext");
  91.         }
  92.     },
  93.  
  94.     updateTimeInfo: function()
  95.     {
  96.         var showTime = Firebug.getPref(this.prefDomain, "trace.showTime");
  97.         if (showTime)
  98.             FBL.setClass(this.logs.firstChild, "showTime");
  99.         else
  100.             FBL.removeClass(this.logs.firstChild, "showTime");
  101.     },
  102.  
  103.     shutdown: function()
  104.     {
  105.         traceService.removeObserver(this, "firebug-trace-on-message");
  106.         prefs.removeObserver(this.prefDomain, this, false);
  107.  
  108.         // Notify listeners
  109.         for (var i=0; i<this.modules.length; ++i)
  110.             this.modules[i].onUnloadConsole(window);
  111.  
  112.         // Unregister from the opener
  113.         if (this.addedOnCloseOpener)
  114.         {
  115.             window.opener.removeEventListener("close", this.onCloseOpener, true);
  116.             delete this.addedOnCloseOpener;
  117.         }
  118.     },
  119.  
  120.     onCloseOpener: function()
  121.     {
  122.         window.close();
  123.     },
  124.  
  125.     registerModule: function(traceModule)
  126.     {
  127.         this.modules.push(traceModule);
  128.     },
  129.  
  130.     unregisterModule: function(module)
  131.     {
  132.         for (var i=0; i<this.modules.length; ++i) {
  133.             if (this.modules[i] == module) {
  134.                 this.modules.splice(i, 1);
  135.                 break;
  136.             }
  137.         }
  138.     },
  139.  
  140.     // nsIObserver
  141.     observe: function(subject, topic, data)
  142.     {
  143.         if (topic == "firebug-trace-on-message")
  144.         {
  145.             // Display messages only with "firebug.extensions" type.
  146.             var messageInfo = subject.wrappedJSObject;
  147.  
  148.             // If the message type isn't specified, use Firebug's pref domain as the default.
  149.             if (!messageInfo.type)
  150.                 messageInfo.type = "extensions.firebug";
  151.  
  152.             if (messageInfo.type != this.prefDomain)
  153.                 return;
  154.  
  155.             var message = new Firebug.TraceModule.TraceMessage(
  156.                 messageInfo.type, data, messageInfo.obj, messageInfo.scope,
  157.                 messageInfo.time);
  158.  
  159.             // If the content isn't loaded yet, remember all messages and insert them later.
  160.             if (this.logs)
  161.                 this.dump(message);
  162.             else
  163.                 queue.push(message);
  164.  
  165.             return true;
  166.         }
  167.         else if (topic == "nsPref:changed")
  168.         {
  169.             if (data == this.prefDomain + ".trace.showTime")
  170.                 this.updateTimeInfo();
  171.         }
  172.     },
  173.  
  174.     // ********************************************************************************************
  175.     // Interface to the output nodes, going by the name outputNodes
  176.     getScrollingNode: function()
  177.     {
  178.         //window.dump(FBL.getStackDump());
  179.         //window.dump("traceConsole getScrollingNode this.scrollingNode "+this.scrollingNode+"\n");
  180.  
  181.         return this.scrollingNode;
  182.     },
  183.     setScrollingNode: function(node)
  184.     {
  185.         this.scrollingNode = node;
  186.     },
  187.     getTargetNode: function()
  188.     {
  189.         //window.dump(FBL.getStackDump());
  190.         //window.dump("traceConsole getTargetgNode this.scrollingNode "+this.logs.firstChild+"\n");
  191.  
  192.         return this.logs.firstChild;
  193.     },
  194.     // ********************************************************************************************
  195.     // Message dump
  196.     dump: function(message)
  197.     {
  198.         // Notify listeners
  199.         for (var i=0; i<this.modules.length; ++i)
  200.             this.modules[i].onDump(message);
  201.  
  202.         Firebug.TraceModule.dump(message, this);
  203.     },
  204.  
  205.     dumpSeparator: function()
  206.     {
  207.         Firebug.TraceModule.MessageTemplate.dumpSeparator(this);
  208.     },
  209.  
  210.     // Trace console toolbar commands
  211.     onClearConsole: function()
  212.     {
  213.         FBL.clearNode(this.logs.firstChild);
  214.     },
  215.  
  216.     onSeparateConsole: function()
  217.     {
  218.         Firebug.TraceModule.MessageTemplate.dumpSeparator(this);
  219.     },
  220.  
  221.     onSaveToFile: function()
  222.     {
  223.         try
  224.         {
  225.             var nsIFilePicker = Ci.nsIFilePicker;
  226.             var fp = Cc["@mozilla.org/filepicker;1"].getService(nsIFilePicker);
  227.             fp.init(window, null, nsIFilePicker.modeSave);
  228.             fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
  229.             fp.filterIndex = 1;
  230.             fp.defaultString = "firebug-tracing-logs.txt";
  231.  
  232.             var rv = fp.show();
  233.             if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace)
  234.             {
  235.                 var foStream = Cc["@mozilla.org/network/file-output-stream;1"]
  236.                     .createInstance(Ci.nsIFileOutputStream);
  237.                 foStream.init(fp.file, 0x02 | 0x08 | 0x20, 0666, 0); // write, create, truncate
  238.  
  239.                 var appInfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
  240.                 var currLocale = Firebug.getPref("general.useragent", "locale");
  241.  
  242.                 // Store head info.
  243.                 var head = "Firebug: " + Firebug.version + "\n" +
  244.                     appInfo.name + ": " + appInfo.version + ", " +
  245.                     appInfo.platformVersion + ", " +
  246.                     appInfo.appBuildID + ", " + currLocale + "\n" +
  247.                     "Export Date: " + (new Date()).toGMTString() +
  248.                     "\n==========================================\n\n";
  249.                 foStream.write(head, head.length);
  250.  
  251.                 // Iterate over all logs and store it into a file.
  252.                 var tbody = this.logs.firstChild;
  253.                 for (var row = tbody.firstChild; row; row = row.nextSibling)
  254.                     this.saveMessage(row.repObject, foStream);
  255.  
  256.                 foStream.close();
  257.             }
  258.         }
  259.         catch (err)
  260.         {
  261.             alert(err.toString());
  262.         }
  263.     },
  264.  
  265.     saveMessage: function(message, stream)
  266.     {
  267.         if (!message)
  268.             return;
  269.  
  270.         var text = message.text;
  271.         text = text ? text.replace(reEndings, "") : "---";
  272.         if (message.type)
  273.             text = "[" + message.type + "] " + text;
  274.         text = (message.index + 1) + ". " + text + "\n"
  275.         stream.write(text, text.length);
  276.         this.saveStackTrace(message, stream);
  277.     },
  278.  
  279.     saveStackTrace: function(message, stream)
  280.     {
  281.         var stack = message.stack;
  282.         for (var i=0; stack && i<stack.length; i++) {
  283.             var frame = stack[i];
  284.             var text = "      " + frame.fileName + " (" + frame.lineNumber + ")\n";
  285.             stream.write(text, text.length);
  286.         }
  287.  
  288.         var end = "\n";
  289.         stream.write(end, end.length);
  290.     },
  291.  
  292.     onRestartFirefox: function()
  293.     {
  294.         Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup).
  295.             quit(Ci.nsIAppStartup.eRestart | Ci.nsIAppStartup.eAttemptQuit);
  296.     },
  297.  
  298.     onExitFirefox: function()
  299.     {
  300.         goQuitApplication();
  301.     },
  302.  
  303. };
  304.  
  305. // ************************************************************************************************
  306.